#include <atomic>
#include <thread>

namespace mipt_parallel {

// naive implementation of Test-And-Set (TAS) spinlock

class tas_spinlock {
public:
    void lock() {
        while (locked_.exchange(true)) {
            std::this_thread::yield();
        }
    }

    bool try_lock() {
        return !locked_.exchange(true);
    }

    void unlock() {
        locked_.store(false);
    }

private:
    std::atomic<bool> locked_;
};

}
